<?php

namespace App\Http\Controllers\Customer;

use App\Http\Controllers\Controller;
use App\Models\client\MsgModel;
use App\Models\client\PriceDetailModel;
use App\Models\client\PriceModel;
use Illuminate\Http\Request;

class Price extends Controller
{

    public function index(Request $request){
        $auth = $request->get('auth');


        $price = PriceModel::with('currency')
            ->where('customer_id', $auth->id)
            ->where('effect_date', '<=', date('Y-m-d'))
            ->orderBy('id', 'desc')
            ->first();

        if(!$price){
            return $this->success();
        }

        $currency_name = $price->currency->short_code ?? '';

        $detail = PriceDetailModel::with('route')->where('price_id', $price->id)->get();

        $return = $detail->map(function($item) use ($currency_name){
            return [
                'route_name' => $item->route->name,
                'goods_attr' => $item->goods_attr == 1 ? '普货' : '特货',
                'price45' => $item->price45,
                'price100' => $item->price100,
                'price300' => $item->price300,
                'price500' => $item->price500,
                'price1000' => $item->price1000,
                'currency_name' => $currency_name,
                'validity' => $item->validity,
                'remark' => $item->remark,
            ];
        });

        return $this->success($return);
    }

}
